// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        //头像预制体
        facePrefab: cc.Prefab,
        //头像列表
        facesNode: cc.Node,
    },
    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        let basePath = 'Canvas/gameBox/commTop/';
        //时间： 00：00
        this.timeStrLabel = cc.find(basePath+'timebg/timeStr').getComponent(cc.Label);
        //总乐豆
        this.douLabel = cc.find(basePath+'beanbg/beanNum').getComponent(cc.Label);
        //获得乐豆
        this.dou2Label = cc.find(basePath+'getXlBeanBox/text').getComponent(cc.Label);        
        //金卡
        this.cardLabel = cc.find(basePath+'cardbg/cardNum').getComponent(cc.Label);
        //钻石卡
        this.zuanLabel = cc.find(basePath+'zuanbg/zuanNum').getComponent(cc.Label);

        this.keyMap = {
            'time': this.timeStrLabel,
            'dou': this.douLabel,
            'dou2': this.dou2Label,
            'jinka': this.cardLabel,
            'zuanka': this.zuanLabel
        }
        this.node.on('updateNum', (map)=>{
            for(let name in map){
                let val = map[name];
                if(name == 'time'){
                    val = this.getTime(val)
                }
                this.keyMap[name].string = val;
            }
        });
        this.node.on('renderFaceList', (obj)=>{
            for(let i=0; i<obj.list.length; i++){
                let item = cc.instantiate(this.facePrefab)
                this.facesNode.addChild(item)
                item.x = i*(item.width-10);
            }
        })
    },
    //补零
    add0(num){
        return num<=9?'0'+num:''+num
    },
    getTime(num){
        let minutes = this.add0(Math.floor(num/60));
        let seconds = this.add0(num%60);
        return minutes+':'+seconds
    },
    start () {

    },

    // update (dt) {},
});
